﻿using System;
using System.Data;
using System.Collections.Generic;
using eTMS.Controllers.KhoaService;
namespace eTMS.Controllers{
public class KhoaControllerBase : CommonController{
	public KhoaControllerBase()
		: base()
	{	
		this.dao = new KhoaServiceClient();
	}
	~KhoaControllerBase(){
		if(dao.State==System.ServiceModel.CommunicationState.Opened)try{dao.Close();}catch{}
	}
	private DataTable _Table;
	private KhoaEntity oKhoa;	
	public KhoaEntity KhoaObject{
		get{
			return oKhoa;
		}
		set{
			oKhoa=value;
		}
	}
	protected KhoaServiceClient dao;
	public DataTable Table{
		get{
			this.LoadAllDSKhoa(false);			
			return _Table;
		}
	}
	public void LoadAllDSKhoa(bool clearCache){
		bool bForce=clearCache;
		if(_Table==null)
		{
			bForce=true;
		}
		if(!bForce)return;
		DataSet ds=dao.SelectAllDS();
		if(ds.Tables.Count>0){
			_Table=ds.Tables[0];
			this.AddIndexToTable(_Table);
		}
	}	
	public void AddIndexToTable(){
		this.AddIndexToTable(_Table);
	}
	public void ResetTableIndex(){
		this.ResetTableIndex(_Table);
	}
	public bool AddNew(){
		if(oKhoa==null)return false;
		bool result=true;
		try{
			oKhoa = dao.SaveRet(oKhoa);
		}
		catch{
			result=false;
		}
		if(result){
			_ListKhoa.Add(oKhoa);
		}
		return result;
	}
	public bool Update(){
		if(oKhoa==null)return false;
		return dao.Update(oKhoa);
	}
	public bool Delete(){
		if(oKhoa==null)return false;
		bool retvalue = dao.Delete(oKhoa);
		if(retvalue){			
			_ListKhoa.Remove(oKhoa);
		}
		return retvalue;
	}
	public bool DeleteByID(Int32 Khoa_ID){
		bool retvalue = dao.DeleteByID(Khoa_ID);		
		if(retvalue){
			KhoaEntity oDeleted=FindKhoa(Khoa_ID);
			if(oDeleted!=null){
				_ListKhoa.Remove(oDeleted);
			}
		}
		return retvalue;
	}
	public void ResetObject(){
		this.oKhoa = new KhoaEntity();
	}
	//Check if exists MaKhoa
	public bool CheckExistsMaKhoa(object value){	
		return dao.SelectOneByMaKhoa(value.ToString()).MaKhoa!=null;
    }    
    //Copy data from data row in grid to object
    public KhoaEntity MapDataRowToObject(DataRow row){
		KhoaEntity o = new KhoaEntity();
		 o.MaKhoa=Convert.ToString(row["MaKhoa"]);
		 o.TenKhoa=Convert.ToString(row["TenKhoa"]);
		 o.MoTaKhoa=Convert.ToString(row["MoTaKhoa"]);
		 if(!row.IsNull("Khoa_ID"))
		    o.Khoa_ID=(Int32)(row["Khoa_ID"]);
		 return o;
    }
	//Copy data from object to data row in grid    
    public void MapObjectToDataRow(KhoaEntity o,DataRow row){
		row["MaKhoa"]=o.MaKhoa;
		row["TenKhoa"]=o.TenKhoa;
		row["MoTaKhoa"]=o.MoTaKhoa;
		row["Khoa_ID"]=o.Khoa_ID;
    }
    public void MapObjectToDataRow(DataRow row){
		this.MapObjectToDataRow(this.KhoaObject,row);
    }
	public void LoadObject(Int32 Khoa_ID){
		KhoaEntity oLoad=FindKhoa(Khoa_ID);
		if(oLoad==null){
			oKhoa=this.LoadKhoa(Khoa_ID);		
			bool bNotIsNull= true;
			 bNotIsNull&=oKhoa.IsNullKhoa_ID;
			if(!bNotIsNull){
				this.ListKhoa.Add(oKhoa);
			}
		}
		else{
			oKhoa=oLoad;
		}
	}
	#region CachedList Khoa
	private static List<KhoaEntity> _ListKhoa=new List<KhoaEntity>();
    public List<KhoaEntity>ListKhoa{
		get{
			return _ListKhoa;
		}
	}
	public static List<KhoaEntity>ListKhoaStatic{
		get{
			return _ListKhoa;
		}
	}
	public KhoaEntity FindKhoa(Int32 Khoa_ID){
		KhoaEntity oResult=this.ListKhoa.Find(delegate(KhoaEntity oFind){
			bool result=true; 
			result&=oFind.Khoa_ID.Equals(Khoa_ID);
			return result;
		 });
		return oResult;
	}
	public static void LoadAllKhoa(bool clearCache){
		if(_ListKhoa.Count==0)
			clearCache=true;
		if(!clearCache)return;		
		KhoaServiceClient dao = new KhoaServiceClient();
		_ListKhoa=new List<KhoaEntity>(dao.SelectAll());
		if(dao.State==System.ServiceModel.CommunicationState.Opened)try{dao.Close();}catch{}
	}
	public KhoaEntity LoadKhoa(Int32 Khoa_ID){			
		KhoaEntity o=dao.SelectOne(Khoa_ID);		
		return o;
	}
	#endregion
}
}
